<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\BidLists;
use App\Models\Demands;
use App\Models\Mails;
use App\Models\Messages;
use App\Models\Stores;
use App\Models\Users;
use App\Models\Wechat;
use Illuminate\Http\Request;

class BidLogController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(Request $request){
        $demand_id = (int)$request->get('demand_id',0);
        $demand_title = Demands::where('id',$demand_id)->value('title');
        return view('admin.demand.bid_list',compact('demand_id','demand_title'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'bid_status' => (int)$request->get('bid_status',0),
                'demand_id' => (int)$request->get('demand_id',0),
            ];
            $lists = BidLists::getList($params);
            //当前需求是否有人中标 0:没有 1:有
            $state = 0;
            foreach ($lists as $k => $v){
                if($v['bid_status'] == 2){
                    $state = 1;
                }
            }
            return $this->writeJson(0,[$lists,$state]);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 中标
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = BidLists::findOrFail($id);
            $demands = Demands::findOrFail($info['demand_id']);
            $userinfo = Users::where('id',$info['store_id'])->first(['id','openid']);
            $update_arr = [
                'bid_status' =>BidLists::BID_STATUS_2,
                'finnshed_time' =>date('Y-m-d H:i:s'),
            ];
            $info->update($update_arr);
            //中标、推送微信消息
            if (!empty($userinfo['openid'])){
                $template_id = 'DLxjkMe8hT04IyrfOGeonVB5xq4n-6BOFLuMR8aH1GY';
                $url = 'http://wap.hrqidian.com/#/mytoubiao';
                $data = [
                    'first' => '恭喜您，您已在众多的竞争者中脱颖而出，一举夺得本次业务！',
                    'keyword1' => $demands['title'],
                    'keyword2' => date('Y-m-d H:i:s'),
                    'keyword3' => $demands['link_name'],
                    'keyword4' => $demands['link_phone'],
                    'remark' => '请立即联系发布者完成业务后续对接。 HR小启客服',
                ];
                Wechat::wechatSend($userinfo['openid'], $template_id, $url, $data);
            }
            //修改其他投标供应商为：未中标
            $list = BidLists::where('demand_id',$info['demand_id'])->where('id','<>',$id)->get(['id']);
            if(!empty($list)){
                foreach ($list as $val){
                    $update = [
                        'bid_status' => BidLists::BID_STATUS_1,
                        'finnshed_time' => date('Y-m-d H:i:s'),
                    ];
                    BidLists::where('id',$val['id'])->Update($update);
                }
            }
            //修改需求状态
            $demands->update(['bid'=>3]);
            if($info['store_id'] != null){
                Stores::where('user_id',$info['store_id'])->increment('service',1);//增加成交量
                //发送站内信-需求订单中标通知
                $mails = [
                    'title' => '需求订单中标通知',
                    'content' => '经平台回访核实，您与' . $info['bid_time'] . '投标的需求达成合作并签订了合同，请及时前往"竞标管理中心"上传合同，谢谢合作。',
                    'user_id' => $info['store_id'],
                    'status' => 1,
                    'type' => 1,
                ];
                Mails::create($mails);
                //发送系统消息
                $message = [
                    'title' => '恭喜' . $info['store_name'] . '成功中标' . $demands['title'],
                    'content' => '恭喜' . $info['store_name'] . '成功中标' . $demands['title'],
                    'type' =>5,
                ];
                Messages::create($message);
            }
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 确认已支付，完成订单
     * @param Request $request
     * @return bool
     */
    public function pay(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = BidLists::findOrFail($id);
            $update_arr = [
                'pay_status' =>BidLists::PAY_STATUS_2,
                'pay_time' =>date('Y-m-d H:i:s'),
            ];
            $info->update($update_arr);
            Demands::where('id',$info['demand_id'])->update(['bid'=>4]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
